//
//  NewHuiYuanViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/3/22.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "NewHuiYuanViewController.h"
#import "JTCSM.h"
#import "MyInfoView.h"
#import "HuiYuanModel.h"
#import "HuankuanJiluTableViewCell.h"
#import "NewJiFenViewController.h"
static CGFloat myinfoHeight = 50;
static CGFloat space = 20;
static CGFloat top = 82;
static CGFloat imageLogoSize = 40;
static CGFloat changeTop = 2.5;
@interface NewHuiYuanViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
{
    UIImageView *headerImageView;
    CGFloat jinduWidth;
    CGFloat titlespace;
    UIScrollView *bottomScrollview;
    UIScrollView *leftBottomScrollview;
}
@property (nonatomic,strong) UIImageView *logoImageV;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *jibieLabel;
@property (nonatomic,strong) UIView *jinduView;
@property (nonatomic,strong) UIImageView *jifenView;
@property (nonatomic,strong) MyInfoView *myinfoView;
@property (nonatomic,strong) UIView *titleLineview;
@property (nonatomic,strong) UILabel *leftLabel;
@property (nonatomic,strong) UILabel *rightLabel;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *datascoureArray;
@end

@implementation NewHuiYuanViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.delegate = self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    
    headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, 198)];
    headerImageView.image = [UIImage imageNamed:@"huiyuan背景.png"];
    [self.view addSubview:headerImageView];
    headerImageView.userInteractionEnabled = YES;
    UIImage * firstImage = [UIImage imageNamed:@"erweimafanhui.png"];
    CGRect firstFrame = CGRectMake(20, 20, 50, 44);
    CGFloat width = firstFrame.size.width;
    CGFloat height = firstFrame.size.height;
    CGFloat imagesize = 20;
    UIButton * leftButton = [[UIButton alloc]initWithFrame:firstFrame];
    [leftButton setImage:firstImage forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(gotBack) forControlEvents:UIControlEventTouchUpInside];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake((height-imagesize)/2, 0, (height-imagesize)/2, (width-imagesize));
    [self.view addSubview:leftButton];
    //    140   150 110  0.35 0.375  0.275
    
    UILabel *titleLabel = [UILabel initWithFrame:CGRectMake(0, 20, KCONTROLLERWith, 44) text:@"天使会员" font:20 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(255, 255, 255, 1) backgroundColor:nil];
    [self.view addSubview:titleLabel];
    [self layoutsubviews];
    [self layoutBottomView];
}
- (void)layoutsubviews{
    
    self.logoImageV = [[UIImageView alloc]initWithFrame:CGRectMake(space, top+changeTop, imageLogoSize, imageLogoSize)];
    self.logoImageV.image = [UIImage imageNamed:@""];
    [headerImageView addSubview:self.logoImageV];
    
    self.nameLabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(self.logoImageV)+15, top, 200, 20) text:@"" font:16 textAlignment:NSTextAlignmentLeft texcolor:[UIColor whiteColor] backgroundColor:nil];
    [headerImageView addSubview:self.nameLabel];
    
    self.jibieLabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(self.logoImageV)+15, top, 200, 20) text:@"" font:10 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:KZhuTiRGB];
    [df layerWithView:self.jibieLabel radius:3 width:0 color:nil];
    [headerImageView addSubview:self.jibieLabel];
    [self changeFrameWithName:@"天使会员" jibie:@"一级铂金"];
    jinduWidth = KCONTROLLERWith-(KVIEW_X(self.logoImageV)+15+20);
    UIView *jinduBackView = [[UIView alloc]initWithFrame:CGRectMake(KVIEW_X(self.logoImageV)+15, KVIEW_Y(self.nameLabel)+10,jinduWidth , 20)];
    jinduBackView.backgroundColor = [df colorWithHexString:@"e09834" alpha:1];
    [headerImageView addSubview:jinduBackView];
    [df layerWithView:jinduBackView radius:10 width:1 color:[UIColor whiteColor]];
    
    
    self.jinduView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith-(KVIEW_X(self.logoImageV)+15+20), 20)];
    [jinduBackView addSubview:self.jinduView];
    [df layerWithView:self.jinduView radius:10 width:1 color:[UIColor whiteColor]];
    CGFloat jinduSpace = (jinduBackView.LC_width-4)/5;
    for (int i = 0; i<4; i++) {
        [df lineViewWithWidthtofLeft:jinduSpace*(i+1) superView:jinduBackView spacing:0 color:[UIColor whiteColor] lineWidth:1];
    }
    
    [self changeJinduFrameWithNumber:0.6];
    
    self.jifenView = [[UIImageView alloc]initWithFrame:CGRectMake(space, headerImageView.LC_height-myinfoHeight/2, KCONTROLLERWith-2*space, myinfoHeight)];
    self.jifenView.image = [UIImage imageNamed:@"积分背景.png"];
    [self.view addSubview:self.jifenView];
    self.jifenView.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(gotoJifenVC) andaddView:self.jifenView];
    
    self.myinfoView = [[MyInfoView alloc]initWithFrame:CGRectMake(0, 0, self.jifenView.LC_width, myinfoHeight-10) imagename:nil title:@"我的积分" placeholder:@"" isShowLabel:ImageandlabelViewType radioBtnIndex:0 GroupId:0];
    self.myinfoView.footerLabel.text = @"134积分";
    self.myinfoView.backgroundColor = [UIColor clearColor];
    self.myinfoView.lineView.hidden = YES;
    self.myinfoView.footerLabel.textColor = [df colorWithHexString:@"ff8a18" alpha:1];
    [self.jifenView addSubview:self.myinfoView];
    self.myinfoView.userInteractionEnabled = YES;
}

- (void)changeFrameWithName:(NSString *)name jibie:(NSString *)jibie{
    self.nameLabel.text = name;
    self.jibieLabel.text = jibie;
    CGRect rect = [df labelHeightWithContent:name height:self.nameLabel.LC_height font:16];
    self.nameLabel.frame = CGRectMake(self.nameLabel.LC_x, self.nameLabel.LC_y, rect.size.width, self.nameLabel.LC_height);
    CGRect rect1 = [df labelHeightWithContent:jibie height:20 font:10];
    self.jibieLabel.frame = CGRectMake(KCONTROLLERWith-rect1.size.width-10-20, self.nameLabel.LC_y, rect1.size.width+10, self.nameLabel.LC_height);
}
#pragma mark 进度长度改变
- (void)changeJinduFrameWithNumber:(CGFloat)number{
    self.jinduView.frame = CGRectMake(0, 0,jinduWidth*number , 20);
    [df jianbianse:self.jinduView beginColor:[df colorWithHexString:@"fee32f" alpha:1] midColor:[df colorWithHexString:@"fcb733" alpha:1] endColor:[df colorWithHexString:@"ff782e" alpha:1] ByGradientType:leftToRight];
}
#pragma mark 跳转积分VC
- (void)gotoJifenVC{
    NewJiFenViewController *vc = [[NewJiFenViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)layoutBottomView{
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(self.jifenView), KCONTROLLERWith, 50)];
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];
    titleView.userInteractionEnabled = YES;
    NSString *title = @"测试数据";
    CGFloat font = 14;
    CGRect rect = [df labelHeightWithContent:title width:100 font:font];
    titlespace = (KCONTROLLERWith/2-rect.size.width)/2;
    self.leftLabel = [UILabel initWithFrame:CGRectMake(titlespace, 0, rect.size.width, titleView.LC_height) text:@"我的权益" font:font textAlignment:NSTextAlignmentCenter texcolor:[df colorWithHexString:@"ff8a18" alpha:1] backgroundColor:nil];
    [titleView addSubview:self.leftLabel];
    self.leftLabel.tag = 100;
    self.leftLabel.userInteractionEnabled = YES;
    
    self.rightLabel = [UILabel initWithFrame:CGRectMake(titlespace+KCONTROLLERWith/2, 0, rect.size.width, titleView.LC_height) text:@"我的任务" font:font textAlignment:NSTextAlignmentCenter texcolor:[df colorWithHexString:@"999999" alpha:1] backgroundColor:nil];
    [titleView addSubview:self.rightLabel];
    self.rightLabel.tag = 101;
    self.rightLabel.userInteractionEnabled = YES;
    
    [df addTapGestureRecognizerWithTarget:self action:@selector(choiceView:) andaddView:self.leftLabel];
    [df addTapGestureRecognizerWithTarget:self action:@selector(choiceView:) andaddView:self.rightLabel];
    
    self.titleLineview = [[UIView alloc]initWithFrame:CGRectMake(titlespace, titleView.LC_height-2, rect.size.width, 2)];
    self.titleLineview.backgroundColor = [df colorWithHexString:@"ff8a18" alpha:1];
    [titleView addSubview:self.titleLineview];
    
    bottomScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, headerImageView.LC_height+myinfoHeight*3/2, KCONTROLLERWith, KCONTROLLERHeight-(headerImageView.LC_height+myinfoHeight*3/2))];
    bottomScrollview.delegate = self;
    bottomScrollview.bounces = NO;
    bottomScrollview.pagingEnabled = YES;
    [bottomScrollview setContentSize:CGSizeMake(KCONTROLLERWith*2, bottomScrollview.LC_height)];
    [self.view addSubview:bottomScrollview];
    
    [self layoutleftView];
    [self layoutTableView];
    
    
}


// scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if ([scrollView isKindOfClass:[UITableView class]]) {
        NSLog(@"------是列表---");
    }
    else if (scrollView == bottomScrollview){
        NSLog(@"------是滚动试图----");
        //    计算页码
        //    页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
        CGFloat scrollviewW =  scrollView.frame.size.width;
        CGFloat x = scrollView.contentOffset.x;
        int page = (x + scrollviewW / 2) /  scrollviewW;
        NSLog(@"%f--%d",x,page);
       [self showViewWithTag:page];
    }
    
}

- (void)choiceView:(UITapGestureRecognizer *)tap{
    NSInteger tag = [tap view].tag-100;
    [self showViewWithTag:tag];
    [bottomScrollview setContentOffset:CGPointMake(KCONTROLLERWith*tag, 0) animated:YES];
}

- (void)showViewWithTag:(NSInteger)tag{
    if (tag == 0) {
        self.leftLabel.textColor = [df colorWithHexString:@"ff8a18" alpha:1];
        self.rightLabel.textColor = [df colorWithHexString:@"999999" alpha:1];
        self.titleLineview.frame = CGRectMake(titlespace, self.titleLineview.LC_y, self.titleLineview.LC_width, self.titleLineview.LC_height);
    }
    
    if (tag == 1) {
        self.rightLabel.textColor = [df colorWithHexString:@"ff8a18" alpha:1];
        self.leftLabel.textColor = [df colorWithHexString:@"999999" alpha:1];
        self.titleLineview.frame = CGRectMake(titlespace+KCONTROLLERWith/2, self.titleLineview.LC_y, self.titleLineview.LC_width, self.titleLineview.LC_height);
    }
    
}


- (void)layoutQuanYiViewWithTitle:(NSString *)title dataArray:(NSMutableArray *)array top:(CGFloat)mytop {
    
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, mytop, KCONTROLLERWith, myinfoHeight)];
    titleView.backgroundColor =[UIColor whiteColor];
    [leftBottomScrollview addSubview:titleView];
    
    CGFloat titleImageWidth = 3;
    CGFloat titleImageHeight = 15;
    CGFloat changetop =(myinfoHeight-titleImageHeight)/2;
    UIImageView *titelImageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, changetop, titleImageWidth, titleImageHeight)];
    titelImageV.backgroundColor = [df colorWithHexString:@"ff8a18" alpha:1];
    [titleView addSubview:titelImageV];
    
    UILabel *lable = [UILabel initWithFrame:CGRectMake(KVIEW_X(titelImageV)+10, changetop, KCONTROLLERWith-100, titleImageHeight) text:title font:13 textAlignment:NSTextAlignmentLeft texcolor:[df colorWithHexString:@"222222" alpha:1] backgroundColor:nil];
    [titleView addSubview:lable];
    MyInfoView *infoview;
    for (int i = 0; i < array.count; i++) {
        HuiYuanModel *model = array[i];
        infoview = [[MyInfoView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(titleView)+myinfoHeight*i, KCONTROLLERWith, myinfoHeight) imagename:model.huiyuanImageName title:model.huiyuanTitle placeholder:nil isShowLabel:ImageandlabelViewType radioBtnIndex:0 GroupId:nil];
        [leftBottomScrollview addSubview:infoview];
        infoview.footerLabel.textColor = [df colorWithHexString:@"999999" alpha:1];
        infoview.footerLabel.text = model.huiyuanfooterTitle;
        infoview.imageView.frame = CGRectMake(10, 10, 30, 30);
        infoview.footerLabel.textColor = [df colorWithHexString:@"999999" alpha:1];
    }
//    [df newlineViewWithHeightofTop:KVIEW_Y(infoview) superView:leftBottomScrollview spacing:0 color:RGBACOLOR(241, 241, 241, 1) lineHeight:10];
    [leftBottomScrollview setContentSize:CGSizeMake(KCONTROLLERWith, mytop+(array.count+1)*myinfoHeight)];
}

- (void)layoutleftView{
    leftBottomScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, bottomScrollview.LC_height)];
    leftBottomScrollview.delegate = self;
    leftBottomScrollview.bounces = NO;
    leftBottomScrollview.pagingEnabled = YES;
    [bottomScrollview addSubview:leftBottomScrollview];
    NSArray *titlearray = @[@"交易返利",@"转入奖励",@"排行榜奖励",@"排行榜奖励"];
    NSArray *imagearray = @[@"铂金权益_交易返利.png",@"天使权益_转入奖励.png",@"铂金权益_排行榜.png",@"钻石权益_翻牌.png"];
    NSArray *contentarray = @[@"返利0.08%",@"享受转入活动",@"享受“排行榜”",@"享受“翻牌奖励”"];
    NSMutableArray *dataarray = [NSMutableArray array];
    for (int i = 0; i<titlearray.count; i++) {
        HuiYuanModel *model = [[HuiYuanModel alloc]init];
        model.huiyuanTitle = titlearray[i];
        model.huiyuanImageName = imagearray[i];
        model.huiyuanfooterTitle = contentarray[i];
        [dataarray addObject:model];
    }
    [self layoutQuanYiViewWithTitle:@"钻石权益" dataArray:dataarray top:0];
//    [self layoutQuanYiViewWithTitle:@"铂金权益" dataArray:dataarray top:dataarray.count*myinfoHeight+myinfoHeight+10];
}


- (void)layoutTableView{
    
    [self getDatasourceArray];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(KCONTROLLERWith, 0, KCONTROLLERWith, bottomScrollview.LC_height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [bottomScrollview addSubview:_tableview];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setUpTableView];
    //    view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
}

- (void)setUpTableView{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tableview addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    // 上拉加载
    [_tableview addFooterWithTarget:self action:@selector(footerRereshing)];
    
}
- (void)headerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        //        [self theRequestWithKeyWord:mykeyword];
    });
}
- (void)footerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        //        [self loadMoreDataRequestWithKeyWord:mykeyword];
        
    });
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _datascoureArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellid = [NSString stringWithFormat:@"textfield%ld",(long)indexPath.row];
    HuankuanJiluTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[HuankuanJiluTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid cellType:IsButtonType];
    }
    [cell setValueWithModel:indexPath];
    HuiYuanModel *model = _datascoureArray[indexPath.row];
    cell.titleLabel.text = model.huiyuanTitle;
    cell.timeLabel.text = model.huiyuanfooterTitle;
    cell.logoImageView.image = [UIImage imageNamed:model.huiyuanImageName];
    cell.stateLabel.text = @"已完成";
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)getDatasourceArray{
    NSArray *titlearray   = @[@"身份认证",@"钱包提现",@"分享拓展",@"拓客交易",@"收益金额"];
    NSArray *contentarray = @[@"注册并完成认证",@"完成一次提现",@"分享拓展2人",@"完成2笔交易",@"累计收益金额18.8元"];
    NSArray *imagearray   = @[@"天使权益_身份认证.png",@"天使权益_钱包提现.png",@"铂金权益_分享拓展.png",@"天使权益_托客交易.png",@"铂金权益_收益金额.png"];
    _datascoureArray = [NSMutableArray arrayWithCapacity:2];
    for (int i = 0; i<titlearray.count; i++) {
        HuiYuanModel *Model = [[HuiYuanModel alloc]init];
        Model.huiyuanTitle = titlearray[i];
        Model.huiyuanfooterTitle = contentarray[i];
        Model.huiyuanImageName = imagearray[i];
        [_datascoureArray addObject:Model];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
